<template>
  <div>
    <!--搜索表单和添加表单-->
    <div style="display: flex;margin-bottom: 10px">
      <div style="width: 250px">
        <el-button type="primary" style="margin-left: 5px" @click="add">添加新院系</el-button>
        <el-popconfirm title="你确定删除所选院系信息吗？" @confirm="deleteSelected">
          <el-button slot="reference" type="danger" style="margin-left: 5px">删除所选院系</el-button>
        </el-popconfirm>
      </div>
      <div style="flex: 1;text-align: right">
        <el-input v-model="params.dno" clearable placeholder="请输入系编号"
                  style="width: 240px;margin-left: 5px"></el-input>
        <el-input v-model="params.dname" clearable placeholder="请输入系名"
                  style="width: 240px;margin-left: 5px"></el-input>
        <el-button class="el-icon-search" type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
        <el-button class="el-icon-refresh" type="primary" style="margin-left: 5px" @click="reset">重置</el-button>
      </div>
    </div>

    <!--主体-->
    <el-table :data="tableData" ref="multipleTable" @selection-change="handleSelectionChange"
              :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}" stripe>
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="dno" label="系编号"></el-table-column>
      <el-table-column prop="dname" label="系名"></el-table-column>
      <el-table-column prop="dmanager" label="系主任"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <!--scope.row就是当前行数据-->
          <el-button type="primary" @click="edit(scope.row.dno,scope.row.dname)">编辑</el-button>
          <el-popconfirm title="你确定删除该管理员信息吗？" @confirm="del(scope.row.dno)">
            <el-button slot="reference" type="danger" style="margin-left: 5px">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--新增弹出框-->
    <el-dialog title="新增院系信息" :visible.sync="dialogVisibleforadd" width="30%">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="系编号" prop="dno">
          <el-input v-model="form.dno" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="系名" prop="dname">
          <el-input v-model="form.dname" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="系主任" prop="dmanager">
          <el-input v-model="form.dmanager" clearable style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisibleforadd = false">取 消</el-button>
    <el-button type="primary" @click="addSave">确 定</el-button>
  </span>
    </el-dialog>

    <!--编辑弹出框-->
    <el-dialog title="编辑院系信息" :visible.sync="dialogVisibleforedit" width="30%">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="系编号" prop="dno">
          <el-input v-model="form.dno" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="系名" prop="dname">
          <el-input v-model="form.dname" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="系主任" prop="dmanager">
          <el-input v-model="form.dmanager" clearable style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisibleforedit = false">取 消</el-button>
    <el-button type="primary" @click="editSave">确 定</el-button>
  </span>
    </el-dialog>

    <!--分页-->
    <div style="margin-top: 10px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import request1 from "@/utils/request1";

export default {
  name: "Department",
  data() {
    return {
      title: '',
      oldDname: '',
      dialogVisibleforadd: false,
      dialogVisibleforedit: false,
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10,
        dno: '',
        dname: ''
      },
      form: {},
      tableData: [],
      multipleSelection: [],
      rules: {
        dno: [{required: true, message: '请输入系编号', trigger: 'blur'},],
        dname: [{required: true, message: '请输入系名', trigger: 'blur'},],
        dmanager: [{required: true, message: '请输入系主任', trigger: 'blur'},],
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request1.get('/department/page', {params: this.params}).then(res => {
        if (res.code == '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        dno: '',
        dname: '',
      }
      this.load()
    },
    add() {
      this.dialogVisibleforadd = true
      this.form = {}
    },
    addSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request1.post('/department/save', this.form).then(res => {
            if (res.code == '200') {
              this.$notify.success("新增成功")
              this.dialogVisibleforadd = false
              this.load()
            } else {
              this.$notify.error('新增失败')
            }
          })
        } else {
          this.$notify.warning('请填写全部信息')
        }
      })
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },
    del(dno) {
      request1.delete('/department/delete/' + dno).then(res => {
        if (res.code == '200') {
          this.$notify.success('删除成功')
          this.load()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    edit(dno, dname) {
      this.oldDname = dname
      this.dialogVisibleforedit = true
      this.title = '编辑院系信息'
      request1.get('/department/' + dno).then(res => {
        this.form = res.data
      })
    },
    editSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request1.put('/department/update/' + this.oldDname, this.form).then(res => {
            if (res.code == '200') {
              this.$notify.success("编辑成功")
              this.dialogVisibleforedit = false
              this.load()
            } else {
              this.$notify.error('编辑失败')
            }
          })
        } else {
          this.$notify.warning('请填写全部信息')
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteSelected() {
      if (this.multipleSelection.length === 0) {
        this.$notify.success('请勾选要删除的选项')
      } else {
        request1.post('/department/deleteSelected', this.multipleSelection).then(res => {
          if (res.code === '200') {
            this.$notify.success('删除成功')
            this.load()
          } else {
            this.$notify.success('删除失败')
          }
        })
      }
    }
  }
}
</script>
